package util;

import cn.hutool.core.collection.CollectionUtil;
import exception.SoiRuntimeException;

import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @className: BeanUtil
 * @author: ocxz1
 * @date: 2025/3/27 上午11:07
 * @Version: 1.0
 * @description: soi
 */
public class BeanUtil {

    /**
     * 通过ID查找
     * @param list
     * @param ids
     * @return
     * @param <T>
     */
    public static <T,U> List<T> findByIds(List<T> list, List<U> ids) {
        if (list == null || list.isEmpty() || ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }

        // 去空
        list = list.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        ids = ids.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toList());

        ArrayList<T> ts = new ArrayList<>();
        T t1 = list.get(0);
        U id = ids.get(0);

        Field idField = null;
        Object idValue;
        try {
            idField = t1.getClass().getDeclaredField("id");
        } catch (NoSuchFieldException e) {
            throw new SoiRuntimeException("未找到ID字段，请检查", e);
        }

        if (idField.getType() != id.getClass()) {
            throw new SoiRuntimeException("ID字段类型不匹配，请检查");
        }

        for (T t : list) {
            if (t != null) {
                try {
                    idField.setAccessible(true);
                    idValue = idField.get(t);
                } catch (IllegalAccessException e) {
                    throw new SoiRuntimeException("反射id字段异常，请检查！");
                }

                if (Objects.equals(idValue, id)) {
                    ts.add(t);
                }
            }
        }
        return ts;
    }

    public static <T,U> T findById(List<T> list, U id, String... notFindMessages) {
        if (id == null) {
            return null;
        }

        List<T> byIds = findByIds(list, List.of(id));
        if (byIds == null || byIds.isEmpty()) {
            // 如果传入了未找到异常，则直接抛出
            if (notFindMessages != null && notFindMessages.length > 0) {
                StringBuilder sb = new StringBuilder();
                for (String message : notFindMessages) {
                    sb.append(message).append(", ");
                }
                sb.delete(sb.length() - 2, sb.length());
                sb.append(id);
                throw new SoiRuntimeException(sb.toString());
            }
            return null;
        }
        return byIds.get(0);
    }

    public static <T,U> List<T> findList(List<T> list, U value, Function<T,U> getter) {
        if (list == null || list.isEmpty() || value == null || getter == null) {
            return Collections.emptyList();
        }

        List<T> returnList = new ArrayList<>();
        for (T t : list) {
            if (t != null) {
                U v = getter.apply(t);
                if (Objects.equals(v, value)) {
                    returnList.add(t);
                }
            }
        }
        return returnList;
    }
}
